<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="户主姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'householdName', validatorRules.householdName]" placeholder="请输入户主姓名"></a-input>
        </a-form-item>
        <a-form-item label="乡镇" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['fwzId']" :trigger-change="true" dictCode="tb_jrtx_fwz,township_name,id" placeholder="请选择乡镇"/>
        </a-form-item>

        <a-form-item label="自然村" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['fwbId']" :trigger-change="true" dictCode="tb_jrtx_fwb,service_name,id" placeholder="请选择自然村"/>
        </a-form-item>
        <a-form-item label="身份证号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'idNumber', validatorRules.idNumber]" placeholder="请输入身份证号"></a-input>
        </a-form-item>

        <a-form-item label="出生日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择出生日期" v-decorator="[ 'birthDate', validatorRules.birthDate]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <!--        0_男，1_女-->
        <a-form-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['sex']" :trigger-change="true" dictCode="sex" placeholder="请选择性别"/>
        </a-form-item>

        <a-form-item label="家庭人口" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'famillyNumber', validatorRules.famillyNumber]" placeholder="请输入家庭人口"></a-input>
        </a-form-item>

        <a-form-item label="文化程度" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'education', validatorRules.education]" placeholder="请输入文化程度"></a-input>
        </a-form-item>

        <a-form-item label="联系方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'phone', validatorRules.phone]" placeholder="请输入联系方式"></a-input>
        </a-form-item>
<!--        0_AAA+,1_AAA,2_AA,3_A,4_无信等级-->
        <a-form-item label="信用等级" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['creditRating']" :trigger-change="true" dictCode="credit_rating" placeholder="请选择信用等级"/>
        </a-form-item>
          
        <a-form-item label="信用评分" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'creditScore', validatorRules.creditScore]" placeholder="请输入信用评分"></a-input>
        </a-form-item>
          

<!--        0_贫困户，1_非贫困户-->
        <a-form-item label="是否贫困" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['isPoor']" :trigger-change="true" dictCode="is_poor" placeholder="请选择是否贫困"/>
        </a-form-item>
          

<!--        0_脱贫仍享受政策、1_未脱贫、2_稳定脱贫-->
        <a-form-item label="贫困户状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['poorHouseholdStatus']" :trigger-change="true" dictCode="poor_household_status" placeholder="请选择贫困户状态"/>
        </a-form-item>
          
        
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import JDate from '@/components/jeecg/JDate'  
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  
  export default {
    name: "CreditRatingModal",
    components: { 
      JDate,
      JDictSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules:{
        householdName:{},
        creditRating:{},
        creditScore:{},
        fwzId:{},
        fwbId:{},
        isPoor:{},
        idNumber:{},
        birthDate:{},
        sex:{},
        famillyNumber:{},
        education:{},
        phone:{},
        poorHouseholdStatus:{},
        },
        url: {
          add: "/creditsystem/creditRating/add",
          edit: "/creditsystem/creditRating/edit",
        }
     
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'householdName','creditRating','creditScore','fwzId','fwbId','isPoor','idNumber','birthDate','sex','famillyNumber','education','phone','poorHouseholdStatus'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'householdName','creditRating','creditScore','fwzId','fwbId','isPoor','idNumber','birthDate','sex','famillyNumber','education','phone','poorHouseholdStatus'))
      }
      
    }
  }
</script>